package com.example.zhu.controller;

import com.example.zhu.dto.OrderDTO;
import com.example.zhu.entity.Orders;
import com.example.zhu.mapper.FakeMapper;
import com.example.zhu.mapper.OrderCountMapper;
import com.example.zhu.mapper.OrderMapper;
import com.example.zhu.result.Result;
import com.example.zhu.service.Impl.WxPayService;
import com.example.zhu.service.OrderService;
import com.example.zhu.vo.ImageOverVO;
import com.example.zhu.vo.OrderMaxVO;
import com.example.zhu.vo.OrderVO;
import com.example.zhu.vo.OrderVOMax;
import com.wechat.pay.contrib.apache.httpclient.exception.HttpCodeException;
import com.wechat.pay.contrib.apache.httpclient.exception.NotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/order")
@CrossOrigin
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private WxPayService wxPayService;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderCountMapper orderCountMapper;

    @Autowired
    private FakeMapper fakeMapper;

    /**
     * 查询所有订单信息
     *
     * @return
     */
    @GetMapping("/getAll")
    public Result<List<OrderVO>> getAll() {
        List<OrderVO> list = orderService.getAll();
        return Result.success(list);
    }

    @GetMapping("/getAllInfo")
    public Result<OrderMaxVO> getOrders(@RequestParam Integer id) {
        OrderMaxVO orderMaxVO = orderService.getAllInfo(id);
        return Result.success(orderMaxVO);
    }

    /**
     * 根据id查询订单信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getEditInfo")
    public Result<OrderVO> getEditInfo(@RequestParam Integer id) {
        OrderVO orderVO = orderService.getEditInfo(id);
        return Result.success(orderVO);
    }

    /**
     * 更新订单信息
     *
     * @param orderDTO
     * @return
     */
    @PutMapping("/updateEditInfo")
    public Result updateEditInfo(@RequestBody OrderDTO orderDTO) {
        log.info("{}", orderDTO);
        orderService.updateEditInfo(orderDTO);
        return Result.success();
    }

    /**
     * 根据订单号、手机号 查询订单信息
     *
     * @param number
     * @return
     */
    @GetMapping("/getByNumber")
    public Result<List<OrderVO>> selectByNumber(@RequestParam String number, @RequestParam String phone, @RequestParam String outPhone) {
        if (!phone.equals("null")) {
            return Result.error("禁用");
        }
        List<OrderVO> list = orderService.selectByNumber(number, phone, outPhone);
        return Result.success(list);
    }

    @GetMapping("/getByTime")
    public Result<List<OrderVOMax>> getByTime(@RequestParam Long start, @RequestParam Long end) {
        List<OrderVOMax> list = orderService.selectByTime(start, end);
        return Result.success(list);
    }

    @GetMapping("/get")
    public Result<List<Orders>> get() {
        List<Orders> list = orderService.selectOrders();
        return Result.success(list);
    }


    /**
     * 取消当前订单
     *
     * @return
     */
    @Transactional
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable Integer id) throws GeneralSecurityException, IOException, HttpCodeException, NotFoundException {

        log.info("当前订单id:{}", id);

        Orders orders = orderMapper.selectById(id);

        orderCountMapper.deleteByOrderId(id);

        fakeMapper.deleteByOrderId(id);

        //订单id
        String Out_trade_no = String.valueOf(orders.getNumber());
        //退款id
        String out_refund_no = String.valueOf(orders.getNumber());

        String reason = "代跑跑退款";
        log.info("发起退款请求,退款id：{}", out_refund_no);
        Double amount = orders.getAmount();
        Integer money = (int) (amount * 100);

        //获取支付请求开关
        Boolean refund = wxPayService.refund(Out_trade_no, out_refund_no, reason, String.valueOf(money));
        if (refund) {
            orderMapper.updateCancel(id, 0);
            return Result.success("取消成功");
        }

        return Result.error("取消失败");
    }


    /**
     * 更新订单状态为6 部分取件
     */
    @PutMapping("/update6/{id}")
    public Result update6(@PathVariable Integer id) {
        log.info("当前的订单id,{}:", id);
        return orderService.update6(id);
    }

    /**
     * 更新订单状态为21 房间内
     *
     * @return
     */
    @PutMapping("/update21/{id}")
    public Result update21(@PathVariable Integer id) {
        log.info("当前的订单id,{}:", id);
        return orderService.update21(id);
    }

    /**
     * 更新订单状态为22 房间外
     */
    @PutMapping("/update22/{id}")
    public Result update22(@PathVariable Integer id) {
        log.info("当前的订单id,{}:", id);
        return orderService.update22(id);
    }

    /**
     * 更新订单状态为23 房间外
     */
    @PutMapping("/update23/{id}")
    public Result update23(@PathVariable Integer id) {
        log.info("当前的订单id,{}:", id);
        return orderService.update23(id);
    }

    /**
     * 更新订单状态为-3 快递柜
     *
     * @param id
     * @return
     */
    @PutMapping("/updatef3/{id}")
    public Result updatef3(@PathVariable Integer id) {
        log.info("当前的订单id,{}:", id);
        return orderService.updatef3(id);
    }

    @PutMapping("/update2/{id}")
    public Result update2(@PathVariable Integer id) {
        return orderService.update2(id);
    }

    /**
     * 修改订单数量
     *
     * @return
     */
    @PutMapping("/updateCount")
    public Result updateCount(@RequestBody Integer id, @RequestBody Integer count) {
        log.info("订单id为:{}", id, count);
        return orderService.updateCount(id, count);
    }

    /**
     * 查询所有完成图片
     */
    @GetMapping("/selectImgOvers")
    public Result<List<ImageOverVO>> selectImgOvers() {
        return orderService.selectImgOvers();
    }


    @GetMapping("/selectIOsByTime/{id}")
    public Result<List<ImageOverVO>> selectIOsByTime(@PathVariable Integer id) {
        log.info("即将查询的订单id,{}", id);
        return orderService.selectIOsByTime(id);
    }

    @PutMapping("/repulse/{id}")
    public Result repulse(@PathVariable Integer id) {
        log.info("订单id,{}", id);
        return orderService.repulse(id);
    }

    @PostMapping("/errorMsg/{id}")
    public Result errorMsg(@PathVariable Integer id, @RequestBody String errorMsg) {
        return orderService.updateErrorMsg(id, errorMsg);
    }


}
